//
//  InformationAlertView.m
//  Peso Guide
//
//  Created by macbook on 2025/6/13.
//

#import "InformationAlertView.h"
#import "DateAlertView.h"
#import "ProductDetailViewController.h"

@interface InformationAlertView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *grayView;
@property (nonatomic, strong) UIImageView *backImg;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *idLabel;
@property (nonatomic, strong) UILabel *DateLabel;
@property (nonatomic, strong) UITextField *nameTF;
@property (nonatomic, strong) UITextField *idTF;
@property (nonatomic, strong) UIButton *dateButton;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIButton *confirmButton;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) DateAlertView *alertView;

@end

@implementation InformationAlertView

- (id)initWithFrame:(CGRect)frame data:(NSMutableArray *)dataArray {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.dataArray = dataArray;
        
        NSDictionary *dataDic1 = [self.dataArray safeObjectAtIndex:0];
        NSDictionary *dataDic2 = [self.dataArray safeObjectAtIndex:1];
        NSDictionary *dataDic3 = [self.dataArray safeObjectAtIndex:2];

        _grayView = [PesoKit createViewWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) backGroundColor:[UIColor colorWithHexString:@"#000000" alpha:0.7]];
        [self addSubview:_grayView];
        
        _backImg = [PesoKit createImageViewWithFrame:CGRectMake((KScreenWidth-301)/2.0, (KScreenHeight-411)/2.0-20, 301, 411) imageNamed:@"ic_identity_alert_back"];
        _backImg.userInteractionEnabled = YES;
        _backImg.clipsToBounds = YES;
        _backImg.layer.cornerRadius = 15.0f;
        [self addSubview:_backImg];
        
        _titleLabel = [PesoKit createLabelWithFrame:CGRectMake(0, 0, _backImg.width, 60) backGroundColor:[UIColor clearColor] text:[Lang set:@"Information confirmation"] textColor:ColorHex(@"#000000") font:FONT(FontWeightStyleMedium, 20)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [_backImg addSubview:_titleLabel];
        
        _messageLabel = [PesoKit createLabelWithFrame:CGRectMake(31, 277, _backImg.width-31*2, 54) backGroundColor:[UIColor clearColor] text:[Lang set:@"Please check your lD information correctly,cesubmitted it is not changed again"] textColor:ColorHex(@"#666660") font:FONT(FontWeightStyleRegular, 12)];
        _messageLabel.numberOfLines = 0;
        [_backImg addSubview:_messageLabel];
        
        _confirmButton = [PesoKit createButtonWithFrame:CGRectMake((_backImg.width-214)/2.0, _backImg.height-32-44, 214, 44) backGroundColor:ColorHex(@"#FE7133") title:[Lang set:@"Confirm"] titleColor:ColorHex(@"#FFFFFF") font:FONT(FontWeightStyleMedium, 18)];
        [_confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _confirmButton.clipsToBounds = YES;
        _confirmButton.layer.cornerRadius = 22.0f;
        [_backImg addSubview:_confirmButton];
        
        _closeButton = [[UIButton alloc] initWithFrame:CGRectMake((KScreenWidth-26)/2.0, _backImg.bottom+16, 26, 26)];
        [_closeButton setBackgroundImage:[UIImage imageNamed:@"ic_identity_close"] forState:0];
        [_closeButton addTarget:self action:@selector(closeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_closeButton];
        
        _nameLabel = [PesoKit createLabelWithFrame:CGRectMake(31, 76, _backImg.width-31*2, 22) backGroundColor:[UIColor clearColor] text:[dataDic1 safeObjectForKey:@"parsing"] textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 16)];
        _nameLabel.numberOfLines = 0;
        [_backImg addSubview:_nameLabel];
        
        _nameTF = [PesoKit createTextFieldWithFrame:CGRectMake(31, _nameLabel.bottom+8, _backImg.width-31*2, 22) backGroundColor:[UIColor clearColor] text:[dataDic1 safeObjectForKey:@"stage"] textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 16) placeholder:@""];
        _nameTF.delegate = self;
        _nameTF.tag = 6346341;
        [_backImg addSubview:_nameTF];
        
        UIView *line1 = [PesoKit createViewWithFrame:CGRectMake(31, _nameTF.bottom+8, _backImg.width-31*2, 0.5) backGroundColor:ColorHex(@"#EEEEEE")];
        [_backImg addSubview:line1];
        
        _idLabel = [PesoKit createLabelWithFrame:CGRectMake(31, line1.bottom+10, _backImg.width-31*2, 22) backGroundColor:[UIColor clearColor] text:[dataDic2 safeObjectForKey:@"parsing"] textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 16)];
        _idLabel.numberOfLines = 0;
        [_backImg addSubview:_idLabel];
        
        _idTF = [PesoKit createTextFieldWithFrame:CGRectMake(31, _idLabel.bottom+8, _backImg.width-31*2, 22) backGroundColor:[UIColor clearColor] text:[dataDic2 safeObjectForKey:@"stage"] textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 16) placeholder:@""];
        _idTF.delegate = self;
        _idTF.tag = 6346342;
        [_backImg addSubview:_idTF];
        
        UIView *line2 = [PesoKit createViewWithFrame:CGRectMake(31, _idTF.bottom+8, _backImg.width-31*2, 0.5) backGroundColor:ColorHex(@"#EEEEEE")];
        [_backImg addSubview:line2];
        
        _DateLabel = [PesoKit createLabelWithFrame:CGRectMake(31, line2.bottom+10, _backImg.width-31*2, 22) backGroundColor:[UIColor clearColor] text:[dataDic3 safeObjectForKey:@"parsing"] textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 16)];
        _DateLabel.numberOfLines = 0;
        [_backImg addSubview:_DateLabel];
        
        _dateButton = [PesoKit createButtonWithFrame:CGRectMake(31, _DateLabel.bottom+8, _backImg.width-31*2, 22) backGroundColor:[UIColor clearColor] title:[dataDic3 safeObjectForKey:@"stage"] titleColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 16)];
        [_dateButton addTarget:self action:@selector(dateButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _dateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_backImg addSubview:_dateButton];
        
        UIView *lineView = [PesoKit createViewWithFrame:CGRectMake(31, _dateButton.bottom+3, _dateButton.width, 1) backGroundColor:ColorHex(@"#EEEEEE")];
        [_backImg addSubview:lineView];
    }
    return self;
}

- (void)dateButtonClick {
    WeakSelf;
    
    _alertView = [[DateAlertView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    _alertView.selectionHandler = ^(NSDictionary * _Nonnull selectedDict) {
        
        NSString *date = [NSString stringWithFormat:@"%@",[selectedDict safeObjectForKey:@"date"]];
        
        NSMutableDictionary *dataDic1 = [weakSelf.dataArray safeObjectAtIndex:2];
        [dataDic1 setObject:date forKey:@"stage"];
        [weakSelf.dataArray replaceObjectAtIndex:2 withObject:dataDic1];
        [weakSelf.dateButton setTitle:date forState:0];
    };
    [self addSubview:_alertView];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.tag == 6346341) {
        NSMutableDictionary *dataDic1 = [self.dataArray safeObjectAtIndex:0];
        [dataDic1 setObject:textField.text forKey:@"stage"];
        [self.dataArray replaceObjectAtIndex:0 withObject:dataDic1];
    }
    
    if (textField.tag == 6346342) {
        NSMutableDictionary *dataDic2 = [self.dataArray safeObjectAtIndex:1];
        [dataDic2 setObject:textField.text forKey:@"stage"];
        [self.dataArray replaceObjectAtIndex:1 withObject:dataDic2];
    }
}

- (void)confirmButtonClick {
    
    NSDictionary *dataDic1 = [self.dataArray safeObjectAtIndex:0];
    NSDictionary *dataDic2 = [self.dataArray safeObjectAtIndex:1];
    NSDictionary *dataDic3 = [self.dataArray safeObjectAtIndex:2];
    
    NSString *restriction = [NSString stringWithFormat:@"%@",[dataDic1 safeObjectForKey:@"stage"]];
    NSString *things = [NSString stringWithFormat:@"%@",[dataDic2 safeObjectForKey:@"stage"]];
    NSString *birthday = [NSString stringWithFormat:@"%@",[dataDic3 safeObjectForKey:@"stage"]];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:birthday forKey:@"qualified"];
    [params setObject:things forKey:@"things"];
    [params setObject:restriction forKey:@"restriction"];
    [params setObject:@"11" forKey:@"unboundlocalerror"];
    [params setObject:self.cardType forKey:@"consisting"];

    
    [[AWHttpService service] postWithUrl:@"/trains/jinja" params:params succ:^(id  _Nonnull responseObject) {
        if (SUCCESS) {
            DLog(@"----%@",responseObject);
            
            NSString *startTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"upLogType-3-startTime"];
            NSString *lng = [[NSUserDefaults standardUserDefaults] objectForKey:@"upLogType-3-lng"];
            NSString *lat = [[NSUserDefaults standardUserDefaults] objectForKey:@"upLogType-3-lat"];

            NSString *endTime = [DataPointManager getCurrentTimestamp];
            
            [[DataPointManager sharedManager] senRequestWithType:@"3" startTime:startTime endTime:endTime lng:lng lat:lat orderId:@""];

//            [[DataPointManager sharedManager] logEventWithType:@"3" startTime:startTime endTime:endTime orderId:@""];
            
            if (self.chooseIdHandler) {
                self.chooseIdHandler(YES);
            }
            
            [self removeFromSuperview];
        }else {
            NSString *order = [NSString stringWithFormat:@"%@",[responseObject safeObjectForKey:@"order"]];
            [SVProgressHUD showErrorWithStatus:order];
            [SVProgressHUD dismissWithDelay:3.0f];
            
            if (self.chooseIdHandler) {
                self.chooseIdHandler(NO);
            }

        }
    } fail:^(NSInteger errorType, id  _Nonnull error) {
        
    }];
}

- (void)closeButtonClick {
    [self removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
